package com.geaosu.niubiplus_java;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.geaosu.niubiplus_java.utils.ImageLoader.ImageLoader;
import com.geaosu.niubiplus_java.utils.ImageLoader.OnImageLoadListener;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.RequestCallback;

import java.util.List;

/**
 * 首页
 */
public class MainActivity extends AppCompatActivity {
    private TextView tvLoadImg;
    private ImageView ivImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tvLoadImg = (TextView) findViewById(R.id.tvLoadImg);
        ivImg = (ImageView) findViewById(R.id.ivImg);

        tvLoadImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 先判断是否有存储权限，如果没有就先申请
                String url = "https://s1.hdslb.com/bfs/static/jinkela/space/assets/icon-auth.png";
                ImageLoader imageLoader = new ImageLoader();
                imageLoader.setOnImageLoadListener(new OnImageLoadListener() {
                    @Override
                    public void onSucc(String url, ImageView imageView, Bitmap bitmap, String msg) {
                        Log.d("------>>", "加载成功：" + msg);
                    }

                    @Override
                    public void onErr(String url, ImageView imageView, String errMsg) {
                        Log.d("------>>", "发生错误：" + errMsg);
                        imageView.setImageResource(R.mipmap.ic_launcher);
                    }
                });
                imageLoader.useDiskCache(true);
                imageLoader.loadImage(url, ivImg);
            }
        });

        PermissionX
                .init(this)
                .permissions(
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, @NonNull List<String> grantedList, @NonNull List<String> deniedList) {
                        if (allGranted) {
                            Log.d("------>>", "全部权限都已授权");
                        } else {
                            Log.d("------>>", "部分权限被拒绝");
                        }
                    }
                });

    }
}